package models

import (
	"my-yipuku/db"
)

type User struct {
	ID             int     `json:"id" gorm:"primary_key"`
	Phone          string  `json:"phone"`
	Nickname       string  `json:"nickname"`
	Email          string  `json:"phone"`
	Password       string  `json:"password"`
	Status         int     `json:"status"`
	RoleIds        string  `json:"role_ids"`
	CouponQuantity int     `json:"coupon_quantity"`
	Balance        float64 `json:"balance"`
	Login_at       int64   `json:"login_at"`
	CreatedAt      int64   `json:"created_at"`
	UpdatedAt      int64   `json:"updated_at"`
}

// 创建一个用户
func CreateUser(user User) int {
	db.Db.Create(&user)
	if db.Db.Error != nil {
		return 0
	}
	// 返回主键
	return user.ID
}

// 通过key-value查询用户
func UserByKEY(key string, value interface{}) User {
	var user User
	db.Db.Where(key+" = ? ", value).First(&user)
	return user
}
